package record

import (
	"github.com/jinzhu/gorm"
	"webGoDemo/initConfig"
	"webGoDemo/model"
)

type DaoService struct {
	db *gorm.DB
}

func (s *DaoService) init() {
	s.db = initConfig.GlobalDB
}

func NewDaoService() *DaoService {
	return &DaoService{
		db: initConfig.GlobalDB,
	}
}

func (s *DaoService) List(pageRequest *model.PageRequest) ([]model.Record, error) {

	var records []model.Record
	s.init()
	tx := s.db
	err := tx.Scopes(pageRequest.PaginateGorm()).
		Table("record").Find(&records).Error
	if err != nil {
		return nil, err
	}
	return records, nil
}

func (s *DaoService) Insert(record *model.Record) error {
	s.init()
	err := s.db.Table("record").Create(record).Error
	if err != nil {
		return err
	}
	return nil
}
